package com.hrt.datasender.datadao.service;

import java.util.ArrayList;
import java.util.List;

import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.orm.executer.IORMExecuter;
import com.hrt.datasender.datadao.model.BcDeviceMonitorParamModel;
import com.hrt.db.DBManager;

public final class BcDeviceMonitorParamService {

    private static volatile BcDeviceMonitorParamService instance;

    private BcDeviceMonitorParamService() {

    }

    public static BcDeviceMonitorParamService getInstance() {
        if (instance == null) {
            synchronized (BcDeviceMonitorParamService.class) {
                if (instance == null) {
                    instance = new BcDeviceMonitorParamService();
                }
            }
        }
        return instance;
    }

    /**
     * 获取串口号对应传输的监测项
     * 
     * @param models
     */
    public List<BcDeviceMonitorParamModel> getParamByDeviceId(String deviceId) {

        IORMExecuter ormExecuter = DBManager.getORMExecuter();
        String sql = " SELECT * FROM BC_DEVICE_MONITOR_PARAM WHERE DEVICE_ID = @DEVICE_ID";

        SQLParameterList pc = new SQLParameterList();
        pc.put("DEVICE_ID", deviceId);

        List<BcDeviceMonitorParamModel> models = ormExecuter.getModelListSQL(sql, pc, BcDeviceMonitorParamModel.class);

        return models;
    }

    /**
     * 获取站点所有参数
     * 
     * @return
     */
    public List<BcDeviceMonitorParamModel> getParamByStation() {
        IORMExecuter ormExecuter = DBManager.getORMExecuter();
        String sql = " SELECT t.*,t1.PARAM_NAME,t1.ALARM_UP,t1.ALARM_DOWN,t1.UNIT FROM BC_DEVICE_MONITOR_PARAM t"
                + " left join BC_MONITOR_PARAM_DICT t1 on t.PARAM_CODE_BUS = t1.PARAM_CODE_BUS";

        List<BcDeviceMonitorParamModel> models = ormExecuter.getModelListSQL(sql, null,
                BcDeviceMonitorParamModel.class);

        return models;
    }

    public void deleteByDevice(String deviceId) {

        String sql = " DELETE FROM BC_DEVICE_MONITOR_PARAM WHERE DEVICE_ID = @DEVICE_ID ";
        SQLParameterList pc = new SQLParameterList();
        pc.put("DEVICE_ID", deviceId);

        DBManager.getDataExecuter().delete(sql, pc);
    }

    public void create(BcDeviceMonitorParamModel model) {
        DBManager.getORMExecuter().create(model);
    }

    public void delete(BcDeviceMonitorParamModel model) {
        DBManager.getORMExecuter().delete(model);
    }

    public void update(BcDeviceMonitorParamModel model) {
        DBManager.getORMExecuter().update(model);
    }

    public List<BcDeviceMonitorParamModel> getListByDeviceID(String deviceId) {

        String sql = " SELECT * FROM BC_DEVICE_MONITOR_PARAM WHERE DEVICE_ID = @DEVICE_ID ";

        SQLParameterList pc = new SQLParameterList();
        pc.put("DEVICE_ID", deviceId);

        List<BcDeviceMonitorParamModel> models = DBManager.getORMExecuter().getModelListSQL(sql, pc,
                BcDeviceMonitorParamModel.class);
        return models;

    }

    /**
     * 获取所有参数
     *
     * @return
     */
    public List<String> getMonitorParam() {
        List<String> result = new ArrayList<>();
        IORMExecuter ormExecuter = DBManager.getORMExecuter();
        String sql = " SELECT * FROM BC_DEVICE_MONITOR_PARAM";
        List<BcDeviceMonitorParamModel> models = ormExecuter.getModelListSQL(sql, null,
                BcDeviceMonitorParamModel.class);
        for(BcDeviceMonitorParamModel model : models){
            result.add(model.getParamCodeBus());
        }
        return result;
    }

}